

/**
 * 批量上传费率处理函数 - 私家车意外险
 */
import { uploadData, getPosition, getStruct, pushRateToList, readExel } from '../batch'

//1. 入参：产品编号 + 基本保费 +  数据表格 + 表头配置
//2. 读取文件，计算数据规模，并记录行数与列数
//3. 解析文件，从行到单元格，并保存到数组
//4. 上传数据，同时打印批处理的总数
//5. 异常处理，抛出错误让前端检查


module.exports = function (data) {


    let
        productId = data.productIdOfRate,// 产品id
        productBase = data.productBase,// 基本保额
        columnList = data.columnList,// 每一列代表的信息
        rateList=[];

    console.log(`\n 开始：读取产品编号为 ${productId} 的费率表...\n`);

    // 拿到文件名，并获取文件
    let exel = getStruct(data.downloadUrl);


    // 开始读取 exel 文件
    readExel({
        lines: exel.lines,
        cols: exel.columns,
        table: exel.table,
        fixCol: 1,
        colList: columnList,
        each(val, i, j, colConf, fixVal) {

            let age = fixVal[0]

            let sexId = colConf.sexId == 1 ? "男" : "女";

            let smoke = colConf.smoke;

            //缴纳年份
            let premiumPaymentPeriod = colConf.premiumPaymentPeriod;

            console.log(`\n 进度：读取第 ${i} 行，年龄为 ${age}, ${sexId}, 价格 ${val}, 年限 ${premiumPaymentPeriod}`);

            pushRateToList({
                "regularLifeInsuranceId": productId,
                "age": age,
                "sex": sexId,
                "smoke": smoke,
                "premiumPaymentPeriod": Number(premiumPaymentPeriod),
                "price": val,
                "productBase": productBase,
            }, rateList)

        },
    });


    return uploadData({
        url: "bath/re-life-rates",
        data: rateList,
        successData: {
            code: 0,
            data: {
                id: productId,
                length: rateList.length
            }
        },
        errData: {
            code: -1,
            msg: '接口错误：api/rates_list'
        },
        token: data.token
    })
}




